home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / dosutils.zip / DELTREE.C < prev    next >
Text File  |  1994-07-04  |  4KB  |  214 lines

  1. /*
  2.  *  deltree - Delete a directory tree
  3.  *
  4.  *  Copyright (C) 1994  Troy Rollo <troy@cbme.unsw.EDU.AU>
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <stddef.h>
  24. #include <string.h>
  25. #include <sys/stat.h>
  26. #include <errno.h>
  27. #include <stdlib.h>
  28. #include <io.h>
  29. #include <dir.h>
  30. #include <dos.h>
  31.  
  32. extern    int    _rtl_chmod(const char *pchPath, int mode, ...);
  33.  
  34. static char *
  35. expand_wildcards(char *s)
  36. {
  37.     static    struct find_t info;
  38.     char wildcard[80];
  39.     static char directory[256];
  40.     static char filename[256];
  41.     unsigned attrib;
  42.     char *c1, *c2;
  43.  
  44.     if (s)
  45.     {
  46.         strcpy(wildcard, s);
  47.         strcpy(directory, wildcard);
  48.             if ((c1 = strrchr(directory, '/')) != 0 ||
  49.             (c2 = strrchr(directory, '\\')) != 0)
  50.             {
  51.             if (c1 > c2)
  52.                 c1[1] = '\0';
  53.             else
  54.                 c2[1] = '\0';
  55.         }
  56.         else
  57.             directory[0] = '\0';
  58.         if (! strchr(wildcard, '.'))
  59.             strcat(wildcard, "*.*");
  60.         attrib = _A_NORMAL | _A_SUBDIR | _A_RDONLY;
  61.         if (_dos_findfirst(wildcard, attrib, &info) != 0)
  62.             return NULL;
  63.         while (!strcmp(info.name, ".") || !strcmp(info.name, ".."))
  64.             if (_dos_findnext(&info))
  65.                 return NULL;
  66.     }
  67.     else
  68.     {
  69.         if (_dos_findnext(&info) != 0)
  70.             return NULL;
  71.     }
  72.     strcpy(filename, directory);
  73.     strcat(filename, info.name);
  74.     return filename;
  75. }
  76.  
  77. FILE    *fpOut;
  78.  
  79. void
  80. deltree(char    *pchFile,
  81.     int    yes)
  82. {
  83.     struct find_t info;
  84.     char wildcard[256];
  85.     char filename[256];
  86.     char achLine[256];
  87.     int    result;
  88.     struct    stat    sbuf;
  89.  
  90.     if (*pchFile == '.')
  91.     {
  92.         fprintf(stderr, "Cannot remove %s\n", pchFile);
  93.         exit(1);
  94.     }
  95.     if (stat(pchFile, &sbuf) == -1)
  96.     {
  97.         fprintf(stderr, "%s: %s\n", pchFile, sys_errlist[errno]);
  98.         exit(1);
  99.     }
  100.     if (!(sbuf.st_mode & S_IFDIR))
  101.     {
  102.         fprintf(stderr, "%s: Not a directory\n", pchFile);
  103.         exit(1);
  104.     }
  105.     if (!yes && fpOut)
  106.     {
  107.         fprintf(fpOut, "Delete %s and all its subdirectories? (y/N) ", pchFile);
  108.         gets(achLine);
  109.         if (*achLine != 'Y' && *achLine != 'y')
  110.             return;
  111.     }
  112.     strcpy(wildcard, pchFile);
  113.     strcat(wildcard, "\\*.*");
  114.     for (result = _dos_findfirst(wildcard,
  115.                 _A_NORMAL |
  116.                 _A_RDONLY |
  117.                 _A_HIDDEN |
  118.                 _A_SYSTEM |
  119.                 _A_SUBDIR |
  120.                 _A_ARCH,
  121.                 &info);
  122.          result == 0;
  123.          result = _dos_findnext(&info))
  124.     {
  125.         if (*info.name == '.')
  126.             continue;
  127.         strcpy(filename, pchFile);
  128.         strcat(filename, "\\");
  129.         strcat(filename, info.name);
  130.         if (info.attrib & _A_SUBDIR)
  131.         {
  132.             deltree(filename, 1);
  133.         }
  134.         else
  135.         {
  136.             if (_rtl_chmod(filename, 1, 0) == -1 ||
  137.                 unlink(filename) == -1)
  138.             {
  139.                 fprintf(stderr, "%s: %s\n",
  140.                         filename,
  141.                         sys_errlist[errno]);
  142.                 exit(1);
  143.             }
  144.         }
  145.     }
  146.     if (_rtl_chmod(pchFile, 1, 0) == -1 ||
  147.         rmdir(pchFile) == -1)
  148.     {
  149.         fprintf(stderr, "%s: %s\n",
  150.                 pchFile,
  151.                 sys_errlist[errno]);
  152.         exit(1);
  153.     }
  154. }
  155.  
  156. int
  157. main(    int    argc,
  158.     char    **argv)
  159. {
  160.     int    yes = 0;
  161.     char    *pchFile;
  162.  
  163.     if (!isatty(0) ||
  164.         (!isatty(1) &&
  165.          !isatty(2)))
  166.         yes = 1;
  167.  
  168.     if (isatty(1))
  169.         fpOut = stdout;
  170.     else if (isatty(2))
  171.         fpOut = stderr;
  172.     else
  173.         fpOut = 0;
  174.  
  175.     while (--argc)
  176.     {
  177.         argv++;
  178.         if (**argv == '/' ||
  179.             **argv == '-')
  180.         {
  181.             while (*++*argv)
  182.             {
  183.                 switch(**argv)
  184.                 {
  185.                 case 'y':
  186.                 case 'Y':
  187.                     yes = 1;
  188.                     break;
  189.  
  190.                 case 'i':
  191.                 case 'I':
  192.                     yes = 0;
  193.                     break;
  194.  
  195.                 default:
  196.                     fprintf(stderr, "Usage: deltree [-{yi}] directory ...\n");
  197.                     return 1;
  198.                 }
  199.             }
  200.         }
  201.         else
  202.         {
  203.             for (pchFile = expand_wildcards(*argv);
  204.                  pchFile;
  205.                  pchFile = expand_wildcards(0))
  206.             {
  207.                 deltree(pchFile, yes);
  208.             }
  209.         }
  210.     }
  211.     return 0;
  212. }
  213.  
  214.